import json
import time
# *******************************************
# *******************************************
from time import sleep
from api_audio import AudioPlay
from send_micro_weight import send_weight
# *******************************************
# *******************************************
import numpy as np
start_time = 0.0

def send_azure_blendshape_weight(animation):
    if animation.strip():
        animation=json.loads(animation)
        frameIndex=animation["FrameIndex"]
        weights = np.array(animation["BlendShapes"])
        duration_time = frameIndex/60
        print("weight_num:",len(weights))
        current_time = time.time()
        sleep_time = duration_time-(current_time - start_time)
        print("duration_time",duration_time,"current spend time:",current_time - start_time)
        if sleep_time>0:
            print("begin sleep",sleep_time)
            sleep(sleep_time)
            speed_up = 0
        else:
            speed_up = sleep_time/(len(weights))
        print("more spped",speed_up)
        for i,weight in enumerate(weights):
            if i % 2 == 0:
                continue
            send_weight(weight)
            time.sleep(1/60+speed_up)
        # send_weight([0]*55)

def get_azure_sound_weights(evt):
    send_azure_blendshape_weight(evt.animation)
    pass
speech_key, service_region = "key", "region"
def oneline_run_message():
    from azure.cognitiveservices.speech.audio import AudioOutputConfig
    import azure.cognitiveservices.speech as speechsdk


    speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region)

    speech_config.speech_synthesis_language = "zh-CN"
    speech_config.speech_synthesis_voice_name ="zh-CN-XiaoyouNeural"

    audio_config = AudioOutputConfig(filename="file2.wav",use_default_speaker=True)


    speech_synthesizer = speechsdk.SpeechSynthesizer(speech_config=speech_config, audio_config=audio_config)

    speech_synthesizer.viseme_received.connect(get_azure_sound_weights)
    ssml = '''<speak version="1.0" xmlns="http://www.w3.org/2001/10/synthesis"
           xmlns:mstts="https://www.w3.org/2001/mstts" xml:lang="zh-CN">
        <voice name="zh-CN-YunzeNeural">
            <mstts:viseme type="FacialExpression"/>
            <mstts:express-as style="calm" styledegree="1">
                愿你眼眸有星辰。心中有大海。每一岁都奔走在自己的热爱里。所有的一切都渐入佳境，既要今朝醉。也要万年长! 你的新年好运正在派件。请保持好心情。
新的一年。愿一切困难都能避重就轻。每一次困难都有心中共鸣。山野皆有雾灯。飘摇亦可归舟。所遇皆良善。所行化坦途。
            </mstts:express-as>
        </voice>
    </speak>'''
    # If VisemeID is the only thing you want, you can also use `speak_text_async()`
    global start_time
    start_time = time.time()+2
    result = speech_synthesizer.speak_ssml_async(ssml).get()

fi = None
def generate_audio_and_blendshapes():
    fi = open('blendshape.txt', 'w', encoding='utf8')
    def save_azure_sound_weights(evt):
        animation = evt.animation
        if animation:
            print(animation)
            fi.write(animation)
            fi.write('\n')
            fi.flush()

    from azure.cognitiveservices.speech.audio import AudioOutputConfig
    import azure.cognitiveservices.speech as speechsdk
    speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region)

    speech_config.speech_synthesis_language = "zh-CN"
    speech_config.speech_synthesis_voice_name = "zh-CN-XiaoyouNeural"

    audio_config = AudioOutputConfig(filename="file2.wav")

    speech_synthesizer = speechsdk.SpeechSynthesizer(speech_config=speech_config, audio_config=audio_config)

    speech_synthesizer.viseme_received.connect(save_azure_sound_weights)
    ssml = '''<speak version="1.0" xmlns="http://www.w3.org/2001/10/synthesis"
               xmlns:mstts="https://www.w3.org/2001/mstts" xml:lang="zh-CN">
            <voice name="zh-CN-YunzeNeural">
                <mstts:viseme type="FacialExpression"/>
                <mstts:express-as style="calm" styledegree="1">
                    愿你眼眸有星辰。心中有大海。每一岁都奔走在自己的热爱里。所有的一切都渐入佳境，既要今朝醉。也要万年长! 你的新年好运正在派件。请保持好心情。
    新的一年。愿一切困难都能避重就轻。每一次困难都有心中共鸣。山野皆有雾灯。飘摇亦可归舟。所遇皆良善。所行化坦途。
                </mstts:express-as>
            </voice>
        </speak>'''
    # If VisemeID is the only thing you want, you can also use `speak_text_async()`
    global start_time
    start_time = time.time() + 2
    result = speech_synthesizer.speak_ssml_async(ssml).get()


def outline_run_message():
    player = AudioPlay()
    player.play_wav_file_thread(filename="file2.wav")
    fr = open('blendshape.txt', 'r', encoding='utf8')
    global start_time
    start_time = time.time()
    for each in fr:
        send_azure_blendshape_weight(each)


if __name__ == '__main__':
    outline_run_message()





